home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!mikenann
- From: Michael Glassman and Ann Ross <mikenann@netcom.com>
- Subject: Re: SEX & C++
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <314C48EE.6223@netcom.com>
- Sender: mikenann@netcom10.netcom.com
- Content-Transfer-Encoding: 7bit
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4idbcv$ue2@news7.erols.com> <4ih7gp$tdt@news5.erols.com>
- Mime-Version: 1.0
- Date: Sun, 17 Mar 1996 17:16:30 GMT
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Chris Cobb wrote:
- >
- > OK, so the subject line is only half true. But now that I have your
- > attention, can someone please tell me if the following code works on
- > their compiler and/or if the code is conformant ANSI C++. I have tried
- > it on three recent-version C++ compilers and it only works on one of
- > them. Since this is such a simple example and will only take you a
- > couple of minuites to enter into your compiler, I challenge you all to
- > find out if this works on your compiler.
- >
- > The implications are tremendous. If I have a constant, MYCONST, that is
- > defined in a header xxx.h, and let's say twenty-five other source files
- > use it: if I change MYCONST, then I have to recompile twenty-five source
- > files.
- >
- > In the following scenario, I only have to recompile ONE source file, and
- > all of the rest can be used without recompiling. Try it out!
- >
- > --- xxx.h ---
- > extern const int MYCONST; // can you declare a const extern?
- > --- end xxx.h ---
- >
- > --- xxx.cc ---
- > #include "xxx.h"
- > const int MYCONST = 11; // const defined in one file...
- > --- end xxx.cc ---
- >
- > --- yyy.cc ---
- > #include <iostream.h>
- > #include "xxx.h"
- > main()
- > {
- > char myarray[MYCONST]; // and used in another!
- >
- > cout << "MYCONST is " << MYCONST << endl;
- > cout << "sizeof myarray[] is " << sizeof(myarray) << endl;
- > return 0;
- > }
- > --- end yyy.c ---
- >
- > Chris
- > ccobb@cseg.com
-
- It should not work an any of the compilers. The problem is that 'const' in C++ defaults
- to 'static' which means file scope in this case. You must add 'extern' to the
- declaration and definition, (i.e. the .cc and .h file).
-
- Michael Glassman
-